﻿Imports System.Xml.Linq
Imports System.Text

Namespace Entities
    Public Class VoiceCommandDefinition
        Public Sub New()
            _CommandSets = New Dictionary(Of String, CommandSet)
        End Sub

        Private ReadOnly _CommandSets As IDictionary(Of String, CommandSet)
        Public ReadOnly Property CommandSets As IEnumerable(Of CommandSet)
            Get
                Return _CommandSets.Values
            End Get
        End Property

        Public Function AddCommandSet(commandSet As CommandSet) As Boolean
            If commandSet Is Nothing Then Throw New ArgumentNullException()
            If Not commandSet.Validate Then Throw New ArgumentException("commandSet invalid")
            If _CommandSets.ContainsKey(commandSet.Culture) Then
                Throw New ArgumentException("commandSet already exists")
            Else
                Me._CommandSets(commandSet.Culture) = commandSet
            End If
            Return True
        End Function

        Public Function RemoveCommand(commandSetCulture As String) As Boolean
            If String.IsNullOrWhiteSpace(commandSetCulture) Then Throw New ArgumentNullException()
            If _CommandSets.ContainsKey(commandSetCulture) Then
                _CommandSets.Remove(commandSetCulture)
                Return True
            Else
                Return False
            End If
        End Function

        Public Function GetXml() As String
            If Not Validate() Then Return Nothing
            Dim strBuild As New StringBuilder
            strBuild.AppendFormat("<VoiceCommands xmlns=""http://schemas.microsoft.com/voicecommands/1.0"">")
            strBuild.AppendLine()
            For Each k In _CommandSets.Keys
                _CommandSets(k).Culture = k
                strBuild.AppendLine(_CommandSets(k).GetXml())
            Next
            strBuild.AppendFormat("</VoiceCommands>")
            Return strBuild.ToString()
        End Function

        Public Function Validate() As Boolean

            Return True
        End Function
    End Class
End Namespace